package com.yanzhu.service.impl;

import com.yanzhu.service.LikeService;
import com.yanzhu.service.mapper.LikeMapper;
import com.yanzhu.service.mapper.MessageMapper;
import com.yanzhu.service.mapper.VideoMapper;
import com.yanzhu.service.mapper.WalletMapper;
import com.yanzhu.service.model.Message;
import com.yanzhu.service.model.Upvote;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: 方炎柱
 * @Description:
 * @Date: Created in 21:45 2018/1/21
 * @Modified:
 */
@Service
@AllArgsConstructor
public class LikeServiceImpl implements LikeService {
    private LikeMapper likeMapper;
    private WalletMapper walletMapper;
    private VideoMapper videoMapper;
    private MessageMapper messageMapper;

    @Override
    public int insert(Upvote like) {
        return likeMapper.insert(like);
    }

    @Override
    @Transactional
    public void likeVideo(Upvote like, int like_sum, float like_fee, Message message) throws Exception {
        likeMapper.insert(like);
        videoMapper.updateVideoLike(like.getVid(), like_sum);
        if(message != null)
            messageMapper.insert(message);
//        Wallet wallet = walletMapper.selectBalanceByUid(like.getUid());
//        float balance = wallet != null? wallet.getBalance():0;
//        if(balance >= like_fee) {
//            likeMapper.insert(like);
//            walletMapper.update(like.getUid(), like_fee);
//        }else {
//            throw new Exception("没有足够的余额!");
//        }
    }

    @Override
    public List<Upvote> queryLikeList(long uid) {
        return likeMapper.queryLikeList(uid);
    }

    @Override
    public int deleteLikeById(long id) {
        return likeMapper.deleteLikeById(id);
    }

    @Override
    public int deleteLikeAll(long uid) {
        return likeMapper.deleteLikeAll(uid);
    }

    @Override
    public int queryVideoLikeCount(long vid) {
        return likeMapper.queryVideoLikeCount(vid);
    }

    @Override
    public int queryLikeCount(long vid, long uid) {
        return likeMapper.queryLikeCount(vid, uid);
    }

    @Override
    public int queryVideoLikeUserCount(long vid) {
        return likeMapper.queryVideoLikeUserCount(vid);
    }

    @Override
    public List<String> queryVideoLikeUserAvatar(long vid, int size) {
        return likeMapper.queryVideoLikeUserAvatar(vid, size);
    }


}
